/***********************************************************************
 * Module:  DAOReparation.java
 * Author:  Simon
 * Purpose: Defines the Class DAOReparation
 ***********************************************************************/

package DAO;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import Modele.Reparation;

/** Data Access Object d'accès aux réparations */
public class DAOReparation {
   /** Renvoie la liste de toutes les réparations
    * 
    * @return La liste de réparations */
   public static ArrayList<Reparation> getAllReparation() {
      Statement stat;
		ArrayList<Reparation> reparations = new ArrayList<Reparation>();
		try {

			stat = DAO.getConnection().createStatement();
			stat.executeUpdate("use nemovelo");
			ResultSet res = stat.executeQuery("select * from Reparation");

			Reparation reparation;
			int idReparation, idVelo, idAgent;
			Date dateDeReparation;
			while (res.next()) {

				idReparation = res.getInt("idReparation");
				idVelo = res.getInt("idVelo");
				idAgent = res.getInt("idAgent");
				dateDeReparation = res.getDate("dateDeReparation");

				reparation = new Reparation(idReparation, idAgent, idVelo, dateDeReparation);

				reparations.add(reparation);
			}

		} catch (SQLException e) {
			System.out.println("sql exception");
			while (e != null) {
				System.out.println(e.getErrorCode());
				System.out.println(e.getMessage());
				System.out.println(e.getSQLState());
				e.printStackTrace();
				e = e.getNextException();
			}
		}

		return reparations;
   }
   
   /** Renvoie une réparation en fonction de son identifiant
    * 
    * @param idReparation Identifiant de la réparation demandée
    * @return La réparation demandée */
   public static Reparation getReparationById(int idReparation) {
		Reparation reparation = null;
		Statement stat;

		try {

			stat = DAO.getConnection().createStatement();
			stat.executeUpdate("use nemovelo");
			ResultSet res = stat
					.executeQuery("select * from Reparation where idReparation="
							+ idReparation);

			int idAgent, idVelo;
			Date dateDeReparation;

			if (res.next()) {
				idReparation = res.getInt("idReparation");
				idAgent = res.getInt("idAgent");
				idVelo = res.getInt("idVelo");
				dateDeReparation = res.getDate("dateDeReparation");

				reparation = new Reparation(idReparation, idAgent, idVelo, dateDeReparation);
			}
			
		} catch (SQLException e) {
			System.out.println("sql exception");
			while (e != null) {
				System.out.println(e.getErrorCode());
				System.out.println(e.getMessage());
				System.out.println(e.getSQLState());
				e.printStackTrace();
				e = e.getNextException();
			}
		}

		return reparation;
   }
      
   /** Met à jour une réparation dans la base
    * 
    * @param reparation L'agent de maintenance à mettre à jour */
   public static void updateReparation(Reparation reparation) {
	   PreparedStatement stat;
		try {
			stat = DAO.getConnection().prepareStatement(
					"select * from Reparation where idReparation=?",
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE);

			stat.setInt(1, reparation.getIdReparation());
			ResultSet res = stat.executeQuery();

			if (res.next()) {

				res.updateInt("idVelo", reparation.getIdVelo());
				res.updateInt("idAgent", reparation.getIdAgentMaintenance());
				res.updateDate("dateDeReparation", new java.sql.Date(reparation.getDateDeReparation().getTime()));

				res.updateRow();

			}

		} catch (SQLException e) {
			System.out.println("sql exception");
			while (e != null) {
				System.out.println(e.getErrorCode());
				System.out.println(e.getMessage());
				System.out.println(e.getSQLState());
				e.printStackTrace();
				e = e.getNextException();
			}
		}
   }
   
   /** Supprime une réparation de la base
    * 
    * @param reparation L'agent de maintenance à supprimer */
   public static void deleteReparation(Reparation reparation) {
	   Statement stat;
		try {
			
			stat = DAO.getConnection().createStatement();

			stat.executeUpdate("delete from Reparation where idReparation="
					+ reparation.getIdReparation());

		} catch (SQLException e) {

			System.out.println("sql exception");
			while (e != null) {
				System.out.println(e.getErrorCode());
				System.out.println(e.getMessage());
				System.out.println(e.getSQLState());
				e.printStackTrace();
				e = e.getNextException();
			}
		}
   }
   
   /** Insère un nouvelle réparation dans la base
    * 
    * @param reparation L'agent de maintenance à insérer */
   public static void insertReparation(Reparation reparation) {
	   PreparedStatement stat;
	 		try {
	 			stat = DAO.getConnection().prepareStatement(
	 					"insert into AgentMaintenance (idVelo,idAgent,dateDeReparation) values (?,?,?)");

	 			
	 			stat.setInt(1, reparation.getIdVelo());
	 			stat.setInt(2, reparation.getIdAgentMaintenance());
	 			stat.setDate(3, new java.sql.Date(reparation.getDateDeReparation().getTime()));
	 			
	 			stat.executeUpdate();


	 		} catch (SQLException e) {
	 			System.out.println("sql exception");
	 			while (e != null) {
	 				System.out.println(e.getErrorCode());
	 				System.out.println(e.getMessage());
	 				System.out.println(e.getSQLState());
	 				e.printStackTrace();
	 				e = e.getNextException();
	 			}
	 		}
   }

}